#ifndef __RPS_ACCESS_H__
#define __RPS_ACCESS_H__

#include <signal.h>

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>

#include "../inc/queue.h"
#include "../inc/http_parser.h"

#include "../inc/json/json.h"

#include "../inc/libevent/event.h"
#include "../inc/libevent/event2/thread.h"
#include "../inc/libevent/event2/listener.h"

#include "../inc/hiredis/adapters/libevent.h"

const int THREAD_NUM = 4;
const int BACKLOG = 65535;
const int DATA_BUFFER_SIZE = 2048;
const int max_fds = 500000;

struct redis 
{
	redisAsyncContext* 	ctx;
	int 				status;
	struct event 		ev_check;
	struct event_base*	base;
};

struct thread 
{
	pthread_t pid;
	struct event_base* base;
	struct event notify_event;
	
	int notify_receive_fd;
	int notify_send_fd;
	
	CQ* new_conn_queue;

	http_parser* parser;
	http_parser_settings* settings;
	
	redis* redis_conn;
};

class conn 
{
private:
	conn() {}
public:
	int sfd;
	short which;

	struct event* event;
	
	enum conn_states state;
	struct thread* thread;
	int ev_flags;

	char* r_buf;
	char* r_curr;
	int r_size;
	int r_bytes;

	char* w_buf;
	char* w_curr;
	int w_size;
	int w_bytes;

	char uuid[32];
	char area[64];
	char oemid[64];
	int dev_port;
	int flush_time;
	
	void set_state(enum conn_states state);
	int  try_read_network();
	bool update_event(const int new_flags);
	static void event_handler(const int fd, const short which, void* arg);
	static conn* conn_new(const int sfd, enum conn_states init_state, const int event_flags, struct event_base* base);
};


	
#define evtcp_safe_free(_var, _freefn) do { \
        _freefn((_var));                    \
        (_var) = NULL;                      \
} while (0)


#endif




